package cn.oscar.controller;

import cn.oscar.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import commonUtils.dto.AdminDTO;
import commonUtils.entities.Administrator;
import commonUtils.entities.Restful;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static commonUtils.utils.CURDConstant.SAVE_SUCCESS;

@Slf4j
@RestController
@RequestMapping("/admins")
public class AdminConrtoller {

    @Autowired
    private AdminService adminService;


    /**
     * 员工登录
     * @param request 用于获取登录员工信息的session的值
     * @param administrator 前端传进来的json对象
     * @return 返回登录结果
     */
    @PostMapping("/login")
    public Restful<Administrator> login(HttpServletRequest request, @RequestBody Administrator administrator){
        System.out.println("进入到login方法");
        return adminService.login(request,administrator);
    }

    /**
     * 员工退出
     * @param request 用于获取登录员工信息的session的值，退出时清除该数据
     * @return 返回退出结果
     */
    @PostMapping("/logout")
    public Restful<String> logout(HttpServletRequest request){
        return adminService.logout(request);
    }

    /**
     *添加用户信息
     * @param administrator 前端传来的json对象
     * @return 返回添加结果
     */
    @PostMapping
    public Restful<String> addAdmin(@RequestBody Administrator administrator){
        return adminService.addAdmin(administrator);
    }


    /**
     * 员工信息分页查询
     * @param page 当前页码
     * @param pageSize 一页显示的数据量
     * @param name 筛选的字符串信息
     * @return 返回分页查询结果
     */
    @GetMapping("/page")
    public Restful<Page<AdminDTO>> selectByPage(int page, int pageSize, String name){
        return adminService.selectByPage(page,pageSize,name);
    }

    /**
     * 根据ID修改员工信息
     * @param administrator 传入的json对象
     * @return 返回修改结果
     */
    @PutMapping
    public Restful<String> updateAdmin(@RequestBody Administrator administrator ){
        return adminService.updateAdmin(administrator);
    }


    /**
     * 根据id查询用户信息,返回用户信息
     * @param id 用户id
     * @return 返回用户信息
     */
    @GetMapping("/{id}")
    public Restful<Administrator> selectAdminById(@PathVariable int id){
        return adminService.selectById(id);
    }

    /**
     * 测试案例，查询所有管理员
     * @return 返回所有管理员信息
     */
    @GetMapping("/list")
    public Restful<List<Administrator>> selectAll(){
        List<Administrator> list = adminService.selectAdminList();
        return new Restful<>(SAVE_SUCCESS,list,"发送成功");
    }


}
